package com.retailers.generator.service.Impl;

import com.retailers.generator.bean.TableBean;
import com.retailers.generator.config.Genconfig;
import com.retailers.generator.service.CodeGeneraeService;
import com.retailers.generator.service.MybatisGenService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: honglei
 * @time: 2021/3/26 0026 14:50
 */
public  abstract class AbsMybatisGen implements MybatisGenService {
    @Autowired
    CodeGeneraeService generaeService;


    public Genconfig config=new Genconfig();


    public void setConfig(Genconfig config) {
        this.config = config;
    }


    @Override
    public boolean genCode(List<TableBean> tableNames) throws Exception {
        for (TableBean tableName : tableNames) {
            genCode(tableName);
        }
        return false;
    }

    @Override
    public boolean genCode(TableBean tableBean) throws Exception {
        tableBean.setColumnBeanList(getColumns(tableBean.getTableName()));

        generateModelFile(tableBean);
//        generateMapperFile(tableBean);
////        generateMapperXMLFile(tableBean);
//        generateServiceInterfaceFile(tableBean);
//        generateServiceImplFile(tableBean);
//        generateControllerFile(tableBean);
        return true;
    }



    private void generateModelFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+".java";
        String path = config.getDiskPath() + config.getPackagePath()+"\\domain\\po\\" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("Model.ftl",file,getDataMap(tableBean));
    }
    private void generateMapperFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+"Mapper.java";
        String path = config.getDiskPath() + config.getPackagePath()+"\\mapper\\" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("Mapper.ftl",file,getDataMap(tableBean));
    }

    private void generateMapperXMLFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+"Mapper.xml";
        String path = config.getDiskPath() + "/" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("MapperXml.ftl",file,getDataMap(tableBean));
    }



    private void generateServiceInterfaceFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+"Service.java";
        String path = config.getDiskPath() + config.getPackagePath()+"\\service\\" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("Service.ftl",file,getDataMap(tableBean));
    }

    private void generateServiceImplFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+"ServiceImpl.java";
        String path = config.getDiskPath() + config.getPackagePath()+"\\service\\Impl\\" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("ServiceImpl.ftl",file,getDataMap(tableBean));
    }


    private void generateControllerFile(TableBean tableBean) throws Exception{
        String suffix = tableBean.getClassName()+"Controller.java";
        String path = config.getDiskPath() + config.getPackagePath()+"\\controller\\" + suffix;
        File file = new File(path);
        generaeService.generateFileByTemplate("Controller.ftl",file,getDataMap(tableBean));
    }

    private Map<String,Object> getDataMap(TableBean tableBean){
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("packageName",config.getPackageName());
        dataMap.put("dateTime","2021/3/26 0026 15:32");
        dataMap.put("comments",tableBean.getComments());
        dataMap.put("tableName",tableBean.getTableName());
        dataMap.put("poName",tableBean.getClassName());
        dataMap.put("columnBeanList",tableBean.getColumnBeanList());
        return dataMap;
    }


}
